package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.convenience.adapter.ConvenienceOrderAdapter;
import com.hxd.otc.module.convenience.bean.ConvenienceOrderBean;
import com.hxd.otc.module.convenience.contract.ConvenienceOrderContract;
import com.hxd.otc.module.convenience.presenter.ConvenienceOrderPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

public class ConvenienceOrderActivity extends BaseActivity<ConvenienceOrderPresenter> implements ConvenienceOrderContract.View {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    ConvenienceOrderAdapter orderAdapter;
    int pageNo = 1;
    int pageSize = 15;
    boolean isLoadMore;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_convenience_order;
    }

    @Override
    protected void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                pageNo = pageNo + 1;
                mPresenter.onLoadOrderList(pageSize, pageNo);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = false;
                pageNo = 1;
                mPresenter.onLoadOrderList(pageSize, pageNo);
            }
        });
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });
    }

    @Override
    protected void initData() {
        initAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }

    private void initAdapter() {
        if (orderAdapter == null) {
            orderAdapter = new ConvenienceOrderAdapter();
            orderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(ConvenienceOrderDetailActivity.EXTRA_INFO, orderAdapter.getItem(position));
                    $startActivity(ConvenienceOrderDetailActivity.class, bundle);
                }
            });
        }
        recyclerView.setAdapter(orderAdapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected ConvenienceOrderPresenter createPresenter() {
        return new ConvenienceOrderPresenter(this);
    }


    @Override
    public void onSetAdapter(List<ConvenienceOrderBean> list) {
        if (list == null) {
            return;
        }
        if (isLoadMore) {
            orderAdapter.addData(list);
        } else {
            orderAdapter.setNewData(list);
        }

        if (!isLoadMore) {

            mRefreshLayout.setEnableLoadMore(true);
            if (list != null && list.size() <= 0) {
                orderAdapter.setEmptyView(notDataView);
            }
            orderAdapter.setNewData(list);
        } else {
            if (list == null || list.size() < pageSize) {
                mRefreshLayout.setEnableLoadMore(false);
            } else {
                mRefreshLayout.setEnableLoadMore(true);
            }
            orderAdapter.addData(list);
        }
    }

    @Override
    public void onShowNoMore() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        if (!isLoadMore)
            orderAdapter.setEmptyView(errorView);
    }
}
